//
//  ServingViewController.m
//  suRead
//
//  Created by lidi on 15/11/10.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "ServingViewController.h"
#import "ServiceNews.h"
#import "Masonry.h"
#import "UIViewController+HUD.h"
#import "UIControl+Blocks.h"

@interface ServingViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *infoWebView;
@end

@implementation ServingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    __weak typeof(self) weak_self = self;
    
    UIView *topBarView= [UIView new];
    topBarView.backgroundColor = [UIColor colorWithRed:248/255.0f green:248/255.0f blue:248/255.0f alpha:1.0f];
    [self.view addSubview:topBarView];
    [topBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(topBarView.superview);
        if (iPhoneX) {
            make.height.equalTo(@88.0f);
        } else {
            make.height.equalTo(@64.0f);
        }
    }];
    
    UILabel *linelab = [UILabel new];
    linelab.backgroundColor = [UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:1.0f];
    [topBarView addSubview:linelab];
    [linelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(topBarView);
        make.height.equalTo(@0.5f);
    }];
 
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"button_backward"] forState:UIControlStateNormal];
    [topBarView addSubview:buttonBack];
    [buttonBack addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        //浏览器返回
        if (weak_self.infoWebView.canGoBack) {
            [weak_self.infoWebView goBack];
        } else {
            [weak_self.navigationController popViewControllerAnimated:YES];
            [weak_self dismissViewControllerAnimated:YES completion:^{
            }];
        }
    }];
    [buttonBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topBarView).with.offset(8.0f);
        make.bottom.equalTo(topBarView);
        make.width.equalTo(@28.0f);
        make.height.equalTo(@44.0f);
    }];
    
    UIButton *buttonWebViewBack= [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonWebViewBack setImage:[UIImage imageNamed:@"btn_refresh"] forState:UIControlStateNormal];
    [topBarView addSubview:buttonWebViewBack];
    [buttonWebViewBack addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        [weak_self.infoWebView reload];
    }];
    [buttonWebViewBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(buttonBack.mas_right).with.offset(8.0f);
        make.bottom.equalTo(buttonBack.mas_bottom);
        make.width.equalTo(@28.0f);
        make.height.equalTo(@44.0f);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"服务";
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.f];
    [topBarView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topBarView);
        make.bottom.equalTo(topBarView.mas_bottom).with.offset(-10.0f);
    }];
    
    self.infoWebView = [UIWebView new];
    self.infoWebView.scalesPageToFit = YES;
    self.infoWebView.scrollView.bounces = NO;
    self.infoWebView.delegate=self;
    [self.view addSubview:self.infoWebView];
    [self.infoWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weak_self.view);
        make.top.mas_equalTo(topBarView.mas_bottom);
        if (iPhoneX) {
            make.bottom.equalTo(self.view).offset(-34.0f);
        } else {
            make.bottom.equalTo(self.view);
        }
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSString *servingURL = [NSString stringWithFormat:@"%@%@",URL_BASE,URL_SUB_SERVING];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:servingURL]];
        [weak_self.infoWebView loadRequest:request];

    });
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden= YES;
    //2016-6-22 sah hold left menu viewDidAppear
    NSString* myopen = [AppDelegate sharedAppDelegate].myOpen;
    if([myopen isEqual:@"1"]) {
        //师爱华 -2016-6-22
        [AppDelegate sharedAppDelegate].myOpen=@"0";
        //[[AppDelegate sharedAppDelegate].sideMenuViewController presentLeftMenuViewController];
    }
        
}


- (void)back:(id)sender
{
    if (self.infoWebView.canGoBack) {
        [self.infoWebView goBack];
    }
}

- (void)showLeftMenu:(id)sender
{
    [self.navigationController popViewControllerAnimated:true];
}

@end
